package com.mszlu.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mszlu.blog.common.Result;
import com.mszlu.blog.common.SysUserThreadLocal;
import com.mszlu.blog.dao.CommentDAO;
import com.mszlu.blog.entity.Comment;
import com.mszlu.blog.entity.SysUser;
import com.mszlu.blog.entity.dto.CommentDto;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.service.CommentService;
import com.mszlu.blog.service.SysUserServive;
import com.mszlu.blog.service.ThreadPoolService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentDAO, Comment> implements CommentService {

    @Autowired
    private CommentService commentService;

    @Autowired
    private SysUserServive sysUserServive;

    @Autowired
    private ThreadPoolService threadPoolService;

    @Override
    public Result viewsById(Long id) {

        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comment::getArticleId,id);
        lambdaQueryWrapper.eq(Comment::getLevel,1);
        lambdaQueryWrapper.orderByDesc(Comment::getCreateDate);

        List<CommentDto> commentDtos = commentService.list(lambdaQueryWrapper).stream().map( (item) -> {
            CommentDto commentDto = new CommentDto();
            BeanUtils.copyProperties(item,commentDto);
            SysUser author = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, item.getAuthorId()));
            commentDto.setAuthor(author);

            LambdaQueryWrapper<Comment> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(Comment::getLevel,2);
            lambdaQueryWrapper1.eq(Comment::getParentId,item.getId());
            List<CommentDto> list = commentService.list(lambdaQueryWrapper1).stream().map( (item1) -> {
                CommentDto commentDto1 = new CommentDto();
                BeanUtils.copyProperties(item1,commentDto1);
                SysUser author1 = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, item1.getAuthorId()));
                commentDto1.setAuthor(author1);

                SysUser toUser = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, item1.getToUid()));
                commentDto1.setToUser(toUser);

                return commentDto1;
            }).collect(Collectors.toList());

            commentDto.setChildrens(list);

            return commentDto;
        } ).collect(Collectors.toList());

        return Result.success(commentDtos);
    }

    @Override
    public Result comment(Comment commentParam) {
        String content = commentParam.getContent();
        Long articleId = commentParam.getArticleId();
        Long parentId = commentParam.getParentId();
        Long toUid = commentParam.getToUid();

        SysUser sysUser = SysUserThreadLocal.get();

        Comment comment = new Comment();
        //设置comment内容
        comment.setContent(content);
        comment.setArticleId(articleId);
        comment.setAuthorId(sysUser.getId());
        comment.setCreateDate(System.currentTimeMillis());
        if (parentId == null || parentId == 0) {
            comment.setLevel(1);
            //更新article表的评论数量
            threadPoolService.updateCommentCounts(articleId);
        }else {
            comment.setLevel(2);
        }
        comment.setParentId( parentId == null ? 0 : parentId );

        comment.setToUid( toUid == null ? 0 : toUid );

        this.commentService.save(comment);


        CommentDto commentDto = new CommentDto();
        BeanUtils.copyProperties(comment,commentDto);
        SysUser author = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, comment.getAuthorId()));
        commentDto.setAuthor(author);

        SysUser toUser = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, comment.getToUid()));
        commentDto.setToUser(toUser);

        return Result.success(commentDto);
    }
}
